<template>
  <div>
    <Table :columns="columns" :data="tableData" @on-selection-change="handleSelectionChange"></Table>
    <Page class="page" :page-size="pageSize" :total="total" size="small" @on-change="changePage" />
 </div>
</template>

<script>
import cover2Api from '@/api/cover2'
import treeApi from '@/api/treeApi'
export default {
  data() {
    return {
      selectedRowKeys: [],
      columns: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: '系统名称',
          key: 'sysName',
          align: 'center'
        },
        {
          title: '系统编码',
          key: 'sysCode',
          align: 'center'
        }
      ],
      tableData: [],
      systemVoList: [],
      total: 0,
      pageSize: 10
    }
  },
  props: {
    modelForm: {
      type: Object,
      default: () => {}
    },
    dcPeriod: {
      type: String,
      default: ''
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.selectedRowKeys = val
    },

    save() {
      this.selectedRowKeys.forEach(item => {
        let sys_item = { sysCode: item.sysCode, sysName: item.sysName }
        this.systemVoList.push(sys_item)
      })
      let params = {
        node: {
          id: this.modelForm.id,
          tenantCode: this.modelForm.tenantCode,
          nodeType: 'standard',
          nodePath: this.modelForm.nodePath,
          nodeCode: this.modelForm.nodeCode,
          nodeName: this.modelForm.nodeName,
          parentId: this.modelForm.parentId,
          nodeLevel: 0
        },
        systemVoList: this.systemVoList,
        buCode: this.$store.state.userBu.key,
        dcPeriod: this.dcPeriod || this.$store.state.writeablePeriod,
        treeCode: 'GJ-' + this.$store.state.userBu.key
      }
      treeApi
        .connectSys(params)
        .then(res => {
          if (res.code !== 200) {
            this.$message.error({
              content: res.msg,
              duration: 3
            })
            return
          }
          this.$message.success({
            content: '关联成功',
            duration: 3
          })
          this.modelForm.ext1 = JSON.stringify((this.modelForm.ext1 && JSON.parse(this.modelForm.ext1) || []).concat(this.systemVoList))
          this.$emit('Close', this.modelForm)
        })
    },
    getTableData(currentPage = 1) {
      const params = {
        buCode: this.$store.state.userBu.key,
        buName: this.$store.state.userBu.value,
        dcPeriod: this.dcPeriod,
        currentPage,
        pageSize: this.pageSize
      }
      cover2Api.getConnectSys(params).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    init() {
      this.getTableData()
    },
    changePage(pageIndex) {
      this.getTableData(pageIndex)
    }
  },
  computed: {
    actions() {
      return [
        {
          text: '取消',
          // icon: "edit",
          handle: () => this.$emit('Close'),
          loading: false
        },
        {
          text: '确认',
          theme: 'primary',
          handle: () => {
            this.save('formValidate')
          },
          loading: false
        }
      ]
    }
  },
  created() {
    this.$emit('SetTitle', '关联系统')
    this.$emit('SetPageMinHeight', 500)
    this.$emit('SetPageWidth', 600)
    this.$emit('SetPageActions', this.actions)
    this.init()
  }
  // mounted() {
  //   init()
  // }
}
</script>

<style lang="less" scoped>
.page {
  text-align: end;
  padding: 10px;
}
</style>
